<!DOCTYPE html>
<html lang="en" class="dark">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>叩丁狼电商运营平台</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.ts"></script>
    <script>
      let newArr = {};
      // let newArr = [];
      var student = [
        {
          id: 1,
          parentId: 0,
          name: '王五',
        },
        {
          id: 2,
          parentId: 1,
          name: '小王',
        },
        {
          id: 3,
          parentId: 0,
          name: '张三',
        },
        {
          id: 4,
          parentId: 3,
          name: '小张',
        },
      ];

      for (let i = 0; i < student.length; i++) {
        if (student[i].parentId == 0) {
          // newArr[i]=student[i]   newArr[key]=value 新增属性
          newArr[student[i].id] = student[i];
        } else {
          // 走else的是 小王小张两个对象
          let key = student[i].parentId;
          newArr[key].children = newArr[key].children || [];
          newArr[key].children.push(student[i]);
        }
      }
      // for (let i = 0; i < student.length; i++) {
      //   if (student[i].parentId == 0) {
      //     // newArr[1] --王五  ,newArr[3]--张三
      //     newArr[student[i].id] = student[i];
      //   } else {
      //     // 走else的是 小王小张两个对象
      //     let parentId = student[i].parentId;
      //     newArr[parentId].children = newArr[parentId].children || [];
      //     newArr[parentId].children.push(student[i]);
      //   }
      // }
      // console.log(newArr);
    </script>
  </body>
</html>
<style></style>
